//
//  AIDetailListTableViewCell.swift
//  AIImage
//
//  Created by deng on 2023/12/10.
//

import UIKit
import Reusable
import SnapKit

class AIDetailListTableViewCell: UITableViewCell,Reusable {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupViews()
    }
    
    let nameL = UILabel.init()
    typealias ButtonBlock = (_ btnTag : Int) -> Void
    var callBack: ButtonBlock?
    
    func callBackBlock(block: @escaping ButtonBlock) {
        callBack = block
    }
    
    private func setupViews() {
     
        backgroundColor = .clear
        selectionStyle = .none
        
        let bgView = UIView.init()
        bgView.backgroundColor = .hexString("#313131")
        contentView.addSubview(bgView)
        
        let goBtn = UIButton.init()
        goBtn.setTitle("生成介绍", for: .normal)
        goBtn.titleLabel?.font = .systemFont(ofSize: 24, weight: .bold)
        goBtn.setTitleColor(.hexString("#ff8218"), for: .normal)
        bgView.addSubview(goBtn)
        goBtn.contentHorizontalAlignment = .right
        goBtn.snp.makeConstraints { make in
            make.right.equalTo(-30*DS.s)
            make.centerY.equalToSuperview()
            make.width.equalTo(102*DS.s)
            make.height.equalTo(26*DS.s)
        }
        goBtn.addTarget(self, action: #selector(goClick), for: .touchUpInside)
        
        nameL.font = .systemFont(ofSize: 24, weight: .bold)
        nameL.textColor = .white
        bgView.addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.left.equalTo(20*DS.s)
            make.top.equalTo(22*DS.s)
            make.right.equalTo(goBtn.snp.left).offset(-5*DS.s)
        }
        nameL.numberOfLines = 0
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(20*DS.s)
            make.right.equalTo(-20*DS.s)
            make.top.equalTo(15*DS.s)
            make.bottom.equalTo(nameL.snp.bottom).offset(20*DS.s)
            make.bottom.equalToSuperview().offset(-15*DS.s)
        }
        bgView.layer.cornerRadius = 10
        bgView.layer.masksToBounds = true 
        
    }
    
    @objc func goClick(){
        
        if self.callBack != nil {
            self.callBack!(1)
        }
    }
    
    func setupData(vo:ResultList){
        let score = ((vo.score ?? "0") as NSString).floatValue
        let scoreStr = String.init(format: "%.2f", score*100)
        nameL.text = "\(vo.name ?? "-") \(scoreStr)%"
    }
}
